package com.tbit.main.service.impl;

import com.tbit.main.constant.SysConstant;
import com.tbit.main.dao.log.ReturnBikeMsgLogDao;
import com.tbit.main.pojo.Point;
import com.tbit.main.pojo.ReturnBikeMsgLog;
import com.tbit.main.service.AccountService;
import com.tbit.main.service.DataService;
import com.tbit.main.service.ReturnBikeMsgLogService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.GeoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2023-05-06 15:43
 */
@Service
public class ReturnBikeMsgLogServiceImpl implements ReturnBikeMsgLogService {

    @Autowired
    private CalibrateServiceImpl calibrateService;
    @Autowired
    private ReturnBikeMsgLogDao returnBikeMsgLogDao;
    @Autowired
    private DataService dataService;
    @Autowired
    private AccountService accountService;

    @Override
    public List<ReturnBikeMsgLog> getPos(Double lon, Double lat, Integer day, Integer mapType,
                                         Integer posFrom, Integer parkPointId, Integer inside, Integer returnType, Integer accountId, Integer distance) {
        /**校准经纬度*/
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }
        if (day == null) {
            day = 7;
        }
        //默认展示附近200米
        if (distance == null) {
            distance = 200;
        }

        Map<String, Object> map = new HashMap<>();
        Point point = calibrateService.commonCalibrate(new Point(lon, lat), mapType, SysConstant.ORIGINAL);
        Double area = 0.00001D * distance;
        map.put("lonMax", point.getX() + area);
        map.put("lonMin", point.getX() - area);
        map.put("latMax", point.getY() + area);
        map.put("latMin", point.getY() - area);
        map.put("returnType", returnType);
        map.put("posTime", DateTimeUtil.getDayAgoTime(day));
        map.put("posFrom", posFrom);
        //增加查询条件，走联合索引
        if (accountId != null) {
            //理论上来说，accountId是必传的，但是为了兼容，这里做一下判断
            map.put("accountId", accountId);
            Integer brandId = accountService.getByAccount(accountId);
            map.put("brandId", brandId);
        }
        List<ReturnBikeMsgLog> returnPosLogs = returnBikeMsgLogDao.getPos(map);
        returnPosLogs = returnPosLogs.stream().filter(i -> i.getLon() != null).collect(Collectors.toList());

        /**筛选定位点*/
        if (parkPointId != null && inside != null) {
            List<Point> points = dataService.getParkList(parkPointId);
            /**站内*/
            if (inside == 1) {
                returnPosLogs = returnPosLogs.parallelStream().filter(e -> GeoUtil.IsPtInPoly(new Point(e.getLon(), e.getLat()), points)).collect(Collectors.toList());
            }
            /**站外*/
            else if (inside == 0) {
                returnPosLogs = returnPosLogs.parallelStream().filter(e -> !GeoUtil.IsPtInPoly(new Point(e.getLon(), e.getLat()), points)).collect(Collectors.toList());
            }
        }


        calibrateService.calibrateOffSetReturnBikeMsgLog(returnPosLogs, mapType);

        return returnPosLogs;
    }
}
